package com.rfy.user.controller;


import com.alibaba.fastjson.JSONObject;
import com.rfy.base.entity.response.BaseResponse;
import com.rfy.user.api.UserApi;
import com.rfy.user.entity.User;
import com.rfy.user.request.UserLoginRequest;
import com.rfy.user.service.UserService;
import com.rfy.user.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

import static com.rfy.base.constant.GlobalConstants.SUCCESS;
import static com.rfy.base.constant.UserConstants.*;


/**
 * @author zhanghuiyong
 * @创建时间 2019年07月16日
 * @描述: 用户模块
 **/

@RestController
@CrossOrigin
public class UserRestController implements UserApi {

    @Autowired
    private UserService userService;


    @Override
    @ResponseBody
    public BaseResponse regist(@RequestBody @Valid User user) {
        BaseResponse registBaseResponse = new BaseResponse<>();
        //校验用户名,手机号,邮箱是否是否存在
        try {
            System.out.println("开始用户注册:" + JSONObject.toJSONString(user));
            boolean isHaveUser = userService.queryIsHaveUser(user);
            if (isHaveUser) {
                registBaseResponse.setMessage(USER_IS_HAVE);
                return registBaseResponse;
            }
            userService.registUser(user);
            registBaseResponse.setStatus(SUCCESS);
            registBaseResponse.setMessage(REGIST_SUCCESS);
            //去除密码
            UserUtil.handUserInfo(user);
            registBaseResponse.setResult(user);
            //session操作 暂时忽略
        } catch (Exception e) {
            e.printStackTrace();
            registBaseResponse.setMessage(ERROR + e.getMessage());
        }
        System.out.println("结束用户注册:" + JSONObject.toJSONString(registBaseResponse));
        return registBaseResponse;
    }


    @Override
    @ResponseBody
    public BaseResponse login(@RequestBody @Valid UserLoginRequest loginRequest) {
        BaseResponse loginBaseResponse = new BaseResponse<>();
        System.out.println("开始用户登录:" + JSONObject.toJSONString(loginRequest));
        try {
            List<User> users = userService.queryUserByAction(loginRequest);
            if (null == users || 1 > users.size()) {
                loginBaseResponse.setMessage(LOGIN_FAIL);
                return loginBaseResponse;
            }
            //用户信息处理
            User user = users.get(0);
            UserUtil.handUserInfo(user);
            loginBaseResponse.setStatus(SUCCESS);
            loginBaseResponse.setResult(user);
        } catch (Exception e) {
            e.printStackTrace();
            loginBaseResponse.setMessage(ERROR + e.getMessage());
        }
        System.out.println("结束用户登录:" + JSONObject.toJSONString(loginBaseResponse));
        return loginBaseResponse;
    }

    @Override
    @ResponseBody
    public BaseResponse updateUserInfo(@RequestBody User user) {
        BaseResponse loginBaseResponse = new BaseResponse<>();
        System.out.println("开始更新用户信息:" + JSONObject.toJSONString(user));
        try {
            //用户id不可为空
            if (null == user.getId()) {
                loginBaseResponse.setMessage(USER_ID_IS_NOT_NULL);
                return loginBaseResponse;
            }
            userService.updateUserInfoById(user);
            User newUser = userService.selectUserById(user.getId());
            loginBaseResponse.setStatus(SUCCESS);
            //用户信息处理
            UserUtil.handUserInfo(newUser);
            loginBaseResponse.setMessage(UPDATE_INFO_SUCCESS);
            loginBaseResponse.setResult(newUser);
        } catch (Exception e) {
            e.printStackTrace();
            loginBaseResponse.setMessage(ERROR + e.getMessage());
        }
        System.out.println("结束更新用户信息:" + JSONObject.toJSONString(loginBaseResponse));
        return loginBaseResponse;
    }


}
